<template>
  <el-container>
    <el-header>
      mango
    </el-header>
    <el-main>
      <div>
        <el-row>
          <el-col :span="8" :offset="8">
            <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
              <el-form-item prop="account">
                <el-input
                    v-model="loginForm.account"
                    size="large"
                    placeholder="Account"
                    prefix-icon="Avatar"
                    clearable>
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input
                    v-model="loginForm.password"
                    size="large"
                    placeholder="Password"
                    show-password
                    clearable>
                </el-input>
              </el-form-item>
              <el-form-item style="width:100%;">
                <el-button
                    type="primary"
                    size="large"
                    icon="Finished"
                    @click="login"
                    :disabled=loading
                    round>
                  <span v-if="loading">登陆中...</span>
                  <span v-else>登陆</span>
                </el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>

      </div>
    </el-main>
    <el-footer>
      @mango
    </el-footer>
  </el-container>
</template>

<script>
import {reactive} from "vue"

export default {
  name: "login",
  data() {
    return {
      loading:false,
      loginForm: {
        account: 'admin',
        password: 'admin'
      },
      loginRules: reactive({
        account: [
          { required: true, message: 'Please input Activity account', trigger: 'blur' },
          { min: 5, max: 20, message: 'Length should be 6 to 20', trigger: 'blur' },
        ],
        password: [
          { required: true, message: 'Please input Activity password', trigger: 'blur' },
          { min: 5, max: 20, message: 'Length should be 6 to 20', trigger: 'blur' },
        ],
      })

    }
  },
  methods: {
    /**
     * 登陆按钮事件
     */
    login() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.$store.dispatch('Login',this.loginForm).then(() => {
              this.$router.push( "/mango" ).catch(()=>{});
          }).catch(error => {
            this.loading = false;
          })
        }
      });

    }
  }
}
</script>

<style scoped>
.row-bottom {
  margin: 20px;
}
</style>